package com.itheima.mapper;

import com.github.pagehelper.Page;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.JobCountData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface EmpMapper {
    /**
     * 条件查询员工列表
     * @param empQueryParam
     * @return
     */
    Page<Emp> list(EmpQueryParam empQueryParam);

    /**
     * 新增员工
     * @param emp
     * @return
     */
    //useGeneratedKeys mybatis框架启用生成的主键，数据库的字段必须是主键且自动增长
    //keyProperty 指定主键对应实体类中的属性名
    // Options作用，把数据库中新增加的主键值赋值给实体类中的id属性
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into emp(username,name,gender,phone,job,salary,image,entry_date,dept_id,create_time,update_time) " +
        "values(#{username},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime})")
    int insert(Emp emp);

    /**
     * 批量删除员工信息
     * @param ids
     */
    void deleteByIds(List<Integer> ids);

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @Select("select * from emp where id = #{id}")
    Emp selectById(Integer id);

    Emp selectInfoById(Integer id);

    /**
     * 更新员工信息
     * @param emp
     * @return
     */
    //所有数据都是从前端过来的，以前端为主，前端传什么，就更新什么,即使是空值或空串也要更新为 null或空串。不用动态更新语句
    //@Update("update emp set username = #{username},name = #{name},gender = #{gender},phone = #{phone},job = #{job},salary = #{salary},image = #{image},entry_date = #{entryDate},dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")
    int updateById(Emp emp);
    //3个双引号 jdk15的特性: 文本块
    @Select("""
        select
            case job when 1 then '班主任'
            when 2 then '讲师'
            when 3 then '学工主管'
            when 4 then '教研主管'
            when 5 then '咨询师'
            else '其他'
            end as jobName,
            count(*) as num
        from emp group by job
        order by num
""")
    List<JobCountData> countEmpJobData2();

    @Select("""
        select
            case job when 1 then '班主任'
            when 2 then '讲师'
            when 3 then '学工主管'
            when 4 then '教研主管'
            when 5 then '咨询师'
            else '其他'
            end as pos,
            count(*) as total
        from emp group by job
        order by total
""")
    List<Map> countEmpJobData();

    @Select( """
        select if(gender=1,'男','女') as name,
            count(*) as value
        from emp group by gender
    """)
    List<Map<String, Object>> empGenderData();

    /**
     * 根据用户名和密码查询员工
     * @param emp
     * @return
     */
    @Select("select * from emp where username=#{username} and password=#{password}")
    Emp selectByUsernameAndPassword(Emp emp);

    @Select("select image from emp")
    List<String> selectAllImages();
}
